package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.Md5Util;

/**
 * 视频类型
 * 
 * @author denghb
 * 
 */
public class VideoTypeEntity extends BaseEntity {
	private static final long serialVersionUID = 490472138337484883L;

	public String statusCode = "";
	public String msg = "";

	public String typeName = "";
	public String typeId = "";

	public List<VideoDetailEntity> entities;

	public static VideoTypeEntity parse(JSONObject jsonObject)
			throws JSONException {
		if (jsonObject == null) {
			return null;
		}
		VideoTypeEntity typeEntity = new VideoTypeEntity();

		if (jsonObject.has("typeName")) {
			typeEntity.typeName = jsonObject.getString("typeName");
		}

		if (jsonObject.has("typeId")) {
			typeEntity.typeId = jsonObject.getString("typeId");
		}
		if (jsonObject.has("videos")) {
			typeEntity.entities = new ArrayList<VideoTypeEntity.VideoDetailEntity>();
			JSONArray array = jsonObject.getJSONArray("videos");
			if (array != null && array.length() > 0) {
				for (int i = 0; i < array.length(); i++) {
					JSONObject videoJson = array.getJSONObject(i);
					typeEntity.entities.add(VideoTypeEntity.VideoDetailEntity
							.parse(videoJson));
				}
			}
		}

		return typeEntity;
	}

	/**
	 * 
	 * @author denghb
	 * 
	 */
	public static class VideoDetailEntity extends BaseEntity {
		/**
		 * 
		 */
		private static final long serialVersionUID = 0x11L;
		public String id = "";
		public String picUrl = "";
		public String count = "";
		public String name = "";
		public String introduce = "";
		public String parts = "";
		public String detail = "";

		public List<VideoPartEntity> partEntities;

		public static class VideoPartEntity extends BaseEntity {
			/**
			 * 
			 */
			private static final long serialVersionUID = 0x12L;
			public String id = "";
			public String picUrl = "";
			public String videoUrl = "";
			public String part = "";
			public String videoName = "";// 视频名称

			public String fileSize = "";//视频大小

			public String downloadState = "";
			public int notificationId;
			public String doWhatClick;
			public int progress;

			public static VideoPartEntity parse(JSONObject jsonObject)
					throws JSONException {
				if (jsonObject == null) {
					return null;
				}
				VideoPartEntity partEntity = new VideoPartEntity();
				if (jsonObject.has("id")) {
					partEntity.id = jsonObject.getString("id");
				}

				if (jsonObject.has("picUrl")) {
					partEntity.picUrl = jsonObject.getString("picUrl");
				}
				if (jsonObject.has("videoUrl")) {
					partEntity.videoUrl = jsonObject.getString("videoUrl");
				}
				if (jsonObject.has("part")) {
					partEntity.part = jsonObject.getString("part");
				}
				if (jsonObject.has("fileSize")) {
					partEntity.fileSize = jsonObject.getString("fileSize");
				}

				return partEntity;

			}

			public static String makeVideoFileName(VideoPartEntity partEntity,
					Context applicationContext) {

				return HctConstants.getBooksRootPath(applicationContext)
						+ HctConstants.DEFAULT_VIDEO_STORE_PATH
						+ partEntity.videoName + "_" + partEntity.part + ".mp4";
			}
		}

		public static VideoDetailEntity parse(JSONObject jsonObject)
				throws JSONException {

			if (jsonObject == null) {
				return null;
			}

			VideoDetailEntity detailEntity = new VideoDetailEntity();
			if (jsonObject.has("id")) {
				detailEntity.id = jsonObject.getString("id");
			}
			if (jsonObject.has("picUrl")) {
				detailEntity.picUrl = jsonObject.getString("picUrl");
			}
			if (jsonObject.has("count")) {
				detailEntity.count = jsonObject.getString("count");
			}
			if (jsonObject.has("detail")) {
				detailEntity.detail = jsonObject.getString("detail");
			}
			if (jsonObject.has("name")) {
				detailEntity.name = jsonObject.getString("name");
			}
			if (jsonObject.has("introduce")) {
				detailEntity.introduce = jsonObject.getString("introduce");
			}
			if (jsonObject.has("parts")) {
				detailEntity.parts = jsonObject.getString("parts");
			}

			if (jsonObject.has("videoPartList")) {
				JSONArray jsonArray = jsonObject.getJSONArray("videoPartList");
				if (jsonArray != null && jsonArray.length() > 0) {
					detailEntity.partEntities = new ArrayList<VideoTypeEntity.VideoDetailEntity.VideoPartEntity>();
					for (int i = 0; i < jsonArray.length(); i++) {
						detailEntity.partEntities.add(VideoPartEntity
								.parse(jsonArray.getJSONObject(i)));
					}

				}
			}

			return detailEntity;

		}
	}

}
